package favorite

import (
	"github.com/gin-gonic/gin"
	"strings"

	"com.jiaguyu/dao/db"
	"com.jiaguyu/until"

	"com.jiaguyu/model"

	"com.jiaguyu/id_gen"

	"com.jiaguyu/logger"

	"com.jiaguyu/middleware/account"
)

//创建收藏文件夹
func AddDirHandle(ctx *gin.Context) {
	var favoriteDir model.FavoriteDir
	err := ctx.BindJSON(&favoriteDir)
	if err != nil {
		logger.Error("BindJSON参数问题%v\n", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}
	favoriteDir.DirName = strings.TrimSpace(favoriteDir.DirName)
	if len(favoriteDir.DirName) == 0 {
		logger.Error("没有参数%v\n", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}

	logger.Debug("bind json success ", favoriteDir)
	//生成 不重复id
	dir_id, err := id_gen.GetId()
	if err != nil {
		logger.Error("生成随机id出错%v\n", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	favoriteDir.DirId = int64(dir_id)

	userId, err := account.GetUserId(ctx)
	if err != nil {
		logger.Error("获取userid%v\n", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	favoriteDir.UserId = userId

	//插入数据库
	err = db.CreateFavoriteDir(&favoriteDir)
	if err != nil {
		logger.Error("插入失败 创建数据失败%v\n", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	until.ResponseSuccess(ctx, nil)
}

//收藏文件
func AddHandle(ctx *gin.Context) {

}

//获取搜藏文件夹
func DirListHandle(ctx *gin.Context) {

}

//或去收藏文件夹列表
func ListHandle(ctx *gin.Context) {

}
